
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>operators/validator · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="router.html" />
    
    
    <link rel="prev" href="converter.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    快速入门
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../guides/project.html">
            
                <a href="../guides/project.html">
            
                    
                    项目结构和初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../guides/api.html">
            
                <a href="../guides/api.html">
            
                    
                    API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../guides/doc.html">
            
                <a href="../guides/doc.html">
            
                    
                    生成 API 文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../guides/client.html">
            
                <a href="../guides/client.html">
            
                    
                    生成客户端
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../concepts/cp.html">
            
                <a href="../concepts/cp.html">
            
                    
                    Consumer && Producer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../concepts/operator.html">
            
                <a href="../concepts/operator.html">
            
                    
                    Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../concepts/filter.html">
            
                <a href="../concepts/filter.html">
            
                    
                    Filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../concepts/prefab.html">
            
                <a href="../concepts/prefab.html">
            
                    
                    Prefab
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../concepts/modifier.html">
            
                <a href="../concepts/modifier.html">
            
                    
                    Modifier
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../concepts/middleware.html">
            
                <a href="../concepts/middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../concepts/context.html">
            
                <a href="../concepts/context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../concepts/source.html">
            
                <a href="../concepts/source.html">
            
                    
                    Parameter Generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../concepts/destination.html">
            
                <a href="../concepts/destination.html">
            
                    
                    Destination Handler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../concepts/error.html">
            
                <a href="../concepts/error.html">
            
                    
                    错误包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../concepts/method.html">
            
                <a href="../concepts/method.html">
            
                    
                    方法包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../concepts/configurer.html">
            
                <a href="../concepts/configurer.html">
            
                    
                    配置器机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../concepts/plugin.html">
            
                <a href="../concepts/plugin.html">
            
                    
                    插件机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../concepts/clients.html">
            
                <a href="../concepts/clients.html">
            
                    
                    多客户端整合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    插件
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../plugins/logger.html">
            
                <a href="../plugins/logger.html">
            
                    
                    系统日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../plugins/reqlog.html">
            
                <a href="../plugins/reqlog.html">
            
                    
                    请求日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../plugins/metrics.html">
            
                <a href="../plugins/metrics.html">
            
                    
                    监控指标插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../plugins/tracing.html">
            
                <a href="../plugins/tracing.html">
            
                    
                    请求追踪插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../plugins/profiling.html">
            
                <a href="../plugins/profiling.html">
            
                    
                    性能分析插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../plugins/version.html">
            
                <a href="../plugins/version.html">
            
                    
                    版本信息插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../plugins/healthcheck.html">
            
                <a href="../plugins/healthcheck.html">
            
                    
                    健康检查插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    框架开发者指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="start.html">
            
                <a href="start.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="log.html">
            
                <a href="log.html">
            
                    
                    log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="errors.html">
            
                <a href="errors.html">
            
                    
                    errors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="definition.html">
            
                <a href="definition.html">
            
                    
                    definition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="method.html">
            
                <a href="method.html">
            
                    
                    definition/method
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="converter.html">
            
                <a href="converter.html">
            
                    
                    operators/converter
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.7" data-path="validator.html">
            
                <a href="validator.html">
            
                    
                    operators/validator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="router.html">
            
                <a href="router.html">
            
                    
                    service/router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="service.html">
            
                <a href="service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="nirvana.html">
            
                <a href="nirvana.html">
            
                    
                    nirvana
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="config.html">
            
                <a href="config.html">
            
                    
                    config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="rest.html">
            
                <a href="rest.html">
            
                    
                    rest
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="utils.html">
            
                <a href="utils.html">
            
                    
                    utils
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="command.html">
            
                <a href="command.html">
            
                    
                    cmd/nirvana
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >operators/validator</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="validator-&#x5305;">validator &#x5305;</h1>
<p>validator &#x5305;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x65B9;&#x6CD5;&#x5E2E;&#x52A9;&#x7528;&#x6237;&#x5FEB;&#x901F;&#x751F;&#x6210;&#x7528;&#x4E8E;&#x6821;&#x9A8C;&#x53C2;&#x6570;&#x7684; Operator&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// OperatorKind means opeartor kind. All operators generated in this package</span>
<span class="hljs-comment">// are has kind `validator`.</span>
<span class="hljs-keyword">const</span> OperatorKind = <span class="hljs-string">&quot;validator&quot;</span>

<span class="hljs-comment">// Category distinguishs validation type based on different Validator implementation.</span>
<span class="hljs-keyword">type</span> Category <span class="hljs-keyword">string</span>

<span class="hljs-keyword">const</span> (
    <span class="hljs-comment">// CategoryVar indicates that the validator can validate basic built-in types.</span>
    <span class="hljs-comment">// Types: string, int*, uint*, bool.</span>
    CategoryVar Category = <span class="hljs-string">&quot;Var&quot;</span>
    <span class="hljs-comment">// CategoryStruct indicates that the validator can validate struct.</span>
    CategoryStruct Category = <span class="hljs-string">&quot;Struct&quot;</span>
    <span class="hljs-comment">// CategoryCustom indicates the validator is a custom validator.</span>
    CategoryCustom Category = <span class="hljs-string">&quot;Custom&quot;</span>
)

<span class="hljs-comment">// Validator describes an interface for all validator.</span>
<span class="hljs-keyword">type</span> Validator <span class="hljs-keyword">interface</span> {
    definition.Operator
    <span class="hljs-comment">// Category indicates validator type.</span>
    Category() Category
    <span class="hljs-comment">// Tag returns tag.</span>
    Tag() <span class="hljs-keyword">string</span>
    <span class="hljs-comment">// Description returns description of current validator.</span>
    Description() <span class="hljs-keyword">string</span>
}

<span class="hljs-comment">// NewCustom calls f for validation, using description for doc gen.</span>
<span class="hljs-comment">// User should only do custom validation in f.</span>
<span class="hljs-comment">// Validations which can be done by other way should be done in another Operator.</span>
<span class="hljs-comment">// Exp:</span>
<span class="hljs-comment">// []definition.Operator{NewCustom(f,&quot;custom validation description&quot;)}</span>
<span class="hljs-comment">// f should be func(ctx context.Context, object AnyType) error</span>
<span class="hljs-keyword">func</span> NewCustom(f <span class="hljs-keyword">interface</span>{}, description <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Struct returns an operator to validate a structs exposed fields, and automatically validates nested structs, unless otherwise specified</span>
<span class="hljs-comment">// and also allows passing of context.Context for contextual validation information.</span>
<span class="hljs-keyword">func</span> Struct(instance <span class="hljs-keyword">interface</span>{}) Validator

<span class="hljs-comment">// String creates validator for string type.</span>
<span class="hljs-keyword">func</span> String(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Int creates validator for int type.</span>
<span class="hljs-keyword">func</span> Int(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Int64 creates validator for int64 type.</span>
<span class="hljs-keyword">func</span> Int64(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Int32 creates validator for int32 type.</span>
<span class="hljs-keyword">func</span> Int32(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Int16 creates validator for int16 type.</span>
<span class="hljs-keyword">func</span> Int16(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Int8 creates validator for int8 type.</span>
<span class="hljs-keyword">func</span> Int8(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Byte creates validator for byte type.</span>
<span class="hljs-keyword">func</span> Byte(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Uint creates validator for uint type.</span>
<span class="hljs-keyword">func</span> Uint(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Uint64 creates validator for uint64 type.</span>
<span class="hljs-keyword">func</span> Uint64(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Uint32 creates validator for uint32 type.</span>
<span class="hljs-keyword">func</span> Uint32(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Uint16 creates validator for uint16 type.</span>
<span class="hljs-keyword">func</span> Uint16(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Uint8 creates validator for uint8 type.</span>
<span class="hljs-keyword">func</span> Uint8(tag <span class="hljs-keyword">string</span>) Validator

<span class="hljs-comment">// Bool creates validator for bool type.</span>
<span class="hljs-keyword">func</span> Bool(tag <span class="hljs-keyword">string</span>) Validator
</code></pre>
<p>&#x76EE;&#x524D;&#x652F;&#x6301;&#x4E09;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x9A8C;&#x8BC1;&#xFF0C;&#x5206;&#x522B;&#x5BF9;&#x5E94; golang &#x57FA;&#x7840;&#x7C7B;&#x578B;&#xFF0C;&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;&#x548C;&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x3002;&#x76EE;&#x524D;&#x9A8C;&#x8BC1;&#x65B9;&#x6CD5;&#x57FA;&#x4E8E; <code>gopkg.in/go-playground/validator.v9</code>&#xFF0C;&#x5982;&#x679C;&#x5E0C;&#x671B;&#x4F7F;&#x7528;&#x5176;&#x4ED6;&#x5DE5;&#x5177;&#xFF0C;&#x53EF;&#x4EE5;&#x81EA;&#x5B9A;&#x4E49;&#x4E00;&#x5957;&#x7C7B;&#x4F3C;&#x7684;&#x9A8C;&#x8BC1;&#x51FD;&#x6570;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="converter.html" class="navigation navigation-prev " aria-label="Previous page: operators/converter">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="router.html" class="navigation navigation-next " aria-label="Next page: service/router">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"operators/validator","level":"1.5.7","depth":2,"next":{"title":"service/router","level":"1.5.8","depth":2,"path":"topics/router.md","ref":"topics/router.md","articles":[]},"previous":{"title":"operators/converter","level":"1.5.6","depth":2,"path":"topics/converter.md","ref":"topics/converter.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*"},"file":{"path":"topics/validator.md","mtime":"2019-08-06T09:48:58.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-20T10:33:16.586Z"},"basePath":"..","book":{"language":"zh-hans"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

